﻿
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UI.Web.MVC.Constructor.Common;
using UI.Web.MVC.Constructor.Models;
using DataBaseHelpers;
using DAL.Hibernate.Entities;

namespace UI.Web.MVC.Constructor.Controllers
{
    public class HomeController : Controller
    {
        // 
        // GET: /Home/

        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(CreateHomeModel model)
        {
            using (var r = DBHelper.GetSystemRepository())
            {
                if(!r.Get<Product>().Query().Any(p=>p.Name.ToLower() == model.Name.ToLower()))
                {
                    if(DatabaseHelper.CreateDataBase(model.Name))
                    {
                        string connectionString = string.Format("server=10.7.1.10;database={0}_PROTOTYPE;user=sa;password=123456;", model.Name);
                        r.Get<Product>().Create(new Product { Name = model.Name, ConnectionString = connectionString });
                    }
                }
            }
            return View();
        }

        string GetConPath()
        {
            return ConfigurationManager.AppSettings.Get("connections");
        }
    }
}
